﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Shop.Intermediate;
using Shop.Intermediate.Data;
using Shop.Intermediate.Repository;
using Shop.Core.Configuration;

namespace Shop.Core
{
    [Serializable]
    public class BasicTypeCore:IPageSplit
    {
        private IDataProvider m_dataProvider;
        private IBasicTypeRepository m_btRepository;
        private IBasicDataRepository m_bdRepository;

        public BasicTypeCore()
        {
            this.m_dataProvider = ConfigAccess.CreateDataProvider();
            this.m_btRepository = m_dataProvider.BasicTypeRepository;
            this.m_bdRepository = m_dataProvider.BasicDataRepository;
        }

        public bool Save(IBasicType entity, ref string message)
        {
            bool result = true;
            IBasicType en = m_btRepository.GetEntity(entity.Code);
            if (en!=null)
            {
                result = false;
                message = "存在相同的代码！\\n";
            }
            en = m_btRepository.GetList("Name==@0", new object[] { entity.Name}).FirstOrDefault();
            if (en!=null)
            {
                result = false;
                message = message + "存在相同的名称！";
            }

            if (result == true)
            {
                m_btRepository.Create(entity);
                m_dataProvider.SubmitChanges();
                message = "添加成功！";

            }
            return result;
        }

        public bool Update(IBasicType entity,ref string message)
        {
            bool result = true;
            IBasicType en;
            en = m_btRepository.GetList("Name==@0 && Code!=@1", new object[] { entity.Name,entity.Code }).FirstOrDefault();
            if (en != null)
            {
                result = false;
                message = message + "存在相同的名称！";
            }

            if (result == true)
            {
                m_btRepository.Update(entity);
                m_dataProvider.SubmitChanges();
                message = "更新成功！";
            }
            return result;
        }

        public IBasicType GetEntity(string code)
        {
            IBasicType en = m_btRepository.GetEntity(code);
            if (en == null)
                return null;
            else
                return m_btRepository.GetEntity(code);
        }

        public bool Delete(string code, ref string message)
        {
            bool result = true;
            IBasicType entity = m_btRepository.GetEntity(code);
            if (entity.BasicDataEntity.Count() > 0)
            {
                result = false;
                message = "已经存在基础数据，不能删除";
            }
            else
            {
                m_btRepository.Delete(code);
                m_dataProvider.SubmitChanges();
                message = "删除成功！";
                result = true;
            }
            return result;

        }

        public IBasicType NewBasicType()
        {
            return m_dataProvider.NewBasicType();
        }
        
        public IEnumerable<IBasicType> GetList()
        {
            return m_btRepository.GetList();
        }

        public IEnumerable<IBasicType> GetList(string condition, object[] obj)
        {
            return m_btRepository.GetList(condition, obj);
        }

        public IEnumerable<IBasicType> GetList(string condition, object[] whereObj, string ordering, object[] orderObj)
        {
            return m_btRepository.GetList(condition, whereObj, ordering, orderObj);
        }

        public IEnumerable<IBasicType> GetList(int rownumber, string condition, object[] whereObj, string ordering, object[] orderObj)
        {
            return m_btRepository.GetList(rownumber, condition, whereObj, ordering, orderObj);
        }

        public IEnumerable<IBasicType> PageResultQuery<IBasicType>(string condition, object[] whereObj, string ordering, object[] orderObj, int pageNo, int pageSize, out int pageCount, out int recordCount)
        {
            return m_btRepository.PageResultQuery<IBasicType>(condition, whereObj, ordering, orderObj, pageNo, pageSize, out pageCount, out recordCount);
        }
    }
}
